﻿Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
Public Class CreateUser
    Dim PicPath As String
    Private Sub CreateUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            If Not My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.SpecialDirectories.Temp & "\heroica\") = True Then
                My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.Temp & "\heroica\")
            End If
            choosePictureDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub createAccountBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createAccountBtn.Click
        CreateAccount()
    End Sub
    Private Sub ChoosePicBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChoosePicBtn.Click
        choosePictureDialog.ShowDialog()
        AccountPictureBox.ImageLocation = choosePictureDialog.FileName
        PicPath = choosePictureDialog.SafeFileName
    End Sub
    Public Sub RefreshList()
        Try
            Dim playerXMLSaveFile As XDocument = XDocument.Load(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\.heroica\Accounts\player.xml")
            Dim desc = playerXMLSaveFile.Descendants("Info").FirstOrDefault()
            Dim playerName As String = desc...<name>.Value
            Dim playerLocX As String = desc...<locX>.Value
            Dim playerLocY As String = desc...<locY>.Value
            Dim playerHealth As String = desc...<health>.Value
            If Not My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\.heroica\Accounts\") = True Then
                My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\.heroica\Accounts\")
                Dim playerXMLSaveFileTemplate As XDocument = _
        <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <!--Heroica player save file-->
        <Info>
            <name>DefaultPlayer</name>
            <locX>20</locX>
            <locY>20</locY>
            <health>100</health>
        </Info>
                playerXMLSaveFileTemplate.Save(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\.heroica\Accounts\player.xml")
            Else
                playerXMLSaveFile = <?xml version="1.0" encoding="utf-8" standalone="yes"?>
                                    <!--Heroica player save file-->
                                    <Info>
                                        <name><%= usernameTextBox.Text %></name>
                                        <locX>20</locX>
                                        <locY>20</locY>
                                        <health>100</health>
                                    </Info>
                playerXMLSaveFile.Save(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\.heroica\Accounts\player.xml")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub CreateAccount()
        Try
            If Not passwordTextBox.Text = passwordTextBox1.Text Then
                MsgBox("Passwords didn't match, please retype them.")
            Else
                Dim Ue As New UnicodeEncoding()
                'Retrieve a byte array based on the source text
                Dim ByteSourceText() As Byte = Ue.GetBytes(passwordTextBox.Text)
                'Instantiate an MD5 Provider object
                Dim Md5 As New MD5CryptoServiceProvider()
                'Compute the hash value from the source
                Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
                'And convert it to String format for return
                encryptedTextBox.Text = Convert.ToBase64String(ByteHash)
                Dim playerXMLSaveFile = _
        <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <!--Heroica player save file-->
        <Info>
            <name><%= usernameTextBox.Text %></name>
            <pass><%= encryptedTextBox.Text %></pass>
            <pic><%= PicPath.ToString %></pic>
        </Info>
                playerXMLSaveFile.Save(My.Computer.FileSystem.SpecialDirectories.Temp & "\heroica\" & usernameTextBox.Text & ".xml")
                UploadAccount()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub UploadAccount()
        Try
            Dim PlayerXML As String = My.Computer.FileSystem.SpecialDirectories.Temp + "\heroica\" + usernameTextBox.Text + ".xml"
            Dim FTPFile As String = usernameTextBox.Text + ".xml"
            Dim FTP As String = "ftp://heroica.freehostingcloud.com/Users/"
            Dim FTPUser As String = "Accounts@heroica.freehostingcloud.com"
            Dim FTPPass As String = "isaacval"
            My.Computer.Network.UploadFile(PlayerXML, FTP & FTPFile, FTPUser, FTPPass)
            UploadPic()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub UploadPic()
        Try
            Dim ImageLoc As String = choosePictureDialog.FileName
            Dim FTPimage As String = usernameTextBox.Text + ".png"
            Dim FTPPic As String = "ftp://heroica.freehostingcloud.com/UserPics/"
            Dim FTPUser As String = "Accounts@heroica.freehostingcloud.com"
            Dim FTPPass As String = "isaacval"
            My.Computer.Network.UploadFile(ImageLoc, FTPPic & FTPimage, FTPUser, FTPPass)
            Me.Hide()
            Game.Show()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub CreateUser_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        End
    End Sub
End Class